/**
 * Created by 55456 on 2016/4/9.
 */
var db = require("../../models/index");
var codeConfig = require("../../configs/codeConfig");
var appConfig  = require("../../configs/appConfig");
var auth = require("../../helpers/auth");
var storyAdaptor = require("../../modelsAdaptor/storyAdaptor");

var autoPrefix = appConfig.autoPrefix;

module.exports = (router) => {
    var User = db.models.User;
    var Story = db.models.Story;
    var UserStory = db.models.UserStory;

    router.post(autoPrefix("storylist/:page/:Count"),function *(){
        this.checkBody("userID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = body.userID;
        var page = this.params.page;
        var count = this.params.Count;
        var isOwn = userID === (yield auth.user(this)).id;
        //todo:parse and define return data format out
        var user = yield User.findOne({
            where:{
                id:userID
            },
            attributes: { exclude: ["password"] }
        });
        var stories = yield storyAdaptor.getStoryByUserID(userID,isOwn,page,count);
        this.body = codeConfig.parseMsg({user:user,stories:stories});
    });

    router.post(autoPrefix("story"),function *(){
        this.checkBody("storyID").notEmpty();
        this.checkBody("AuthorID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var isOwn = body.AuthorID === (yield auth.user(this)).id;
        var storyID = body.storyID;
        var story = yield storyAdaptor.getStoryByStoryID(storyID);
        this.body = codeConfig.parseMsg({story:story,isOwn:isOwn});
    });

    router.post(autoPrefix("addstory"),function *(){
        this.checkBody("title").notEmpty();
        this.checkBody("flags").notEmpty();
        this.checkBody("content").notEmpty();
        this.checkBody("publicEnable").notEmpty().toInt();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var story = {
            title:body.title,
            content:body.content,
            flags:body.flags,
            publicEnable : publicEnable
        };
        var data = yield storyAdaptor.AddStory(story);
        this.body = codeConfig.parseMsg(data);
    });

    router.post(autoPrefix("story/like"),function *(){
        this.checkBody("storyID").notEmpty();
        this.checkBody("isLike").notEmpty().toInt();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        try {
            yield UserStory.update({
                isLike: body.isLike
            }, {
                where: {
                    UserId: userID,
                    StoryId: body.storyID
                }
            });
        }catch(err){
            console.log(err);
            this.body = codeConfig.parseErrorMsg(err);
            return;
        }
        this.body = codeConfig.parseMsg(true);
    });

    router.post(autoPrefix("story/collect"),function *(){
        //todo:如果收藏的故事以后被转成private了怎么办
        this.checkBody("storyID").notEmpty();
        this.checkBody("isCollect").notEmpty().toInt();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var body = this.request.body;
        var userID = (yield auth.user(this)).id;
        try {
            yield UserStory.update({
                isCollect: body.isCollect
            }, {
                where: {
                    UserId: userID,
                    StoryId: body.storyID
                }
            });
        }catch(err){
            console.log(err);
            this.body = codeConfig.parseErrorMsg(err);
            return;
        }
        this.body = codeConfig.parseMsg(true);
    });

    router.post(autoPrefix("public/storylist/:page/:Count"),function *(){
        this.checkParams("page").notEmpty().toInt();
        this.checkParams("Count").notEmpty().toInt();
        this.checkBody("timestamps").notEmpty().toString();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
            return;
        }
        var timestamps = this.request.body.timestamps;
        var page = this.params.page;
        var count  = this.params.Count;
        var data = yield storyAdaptor.getAllPublicStories(page,count,timestamps);
        this.body = codeConfig.parseMsg(data);
    });

};